﻿namespace Api.Controllers;

/// <summary>
/// 公共接口
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class CommonController(IConfiguration configuration, IDictonaryBLL dictonaryBLL) : BaseController
{
    private readonly IConfiguration _configuration = configuration;
    private readonly IDictonaryBLL _dictonaryBLL = dictonaryBLL;

    #region 手机号解密
    /// <summary>
    /// 手机号解密
    /// </summary>
    /// <param name="phoneNum"></param>
    /// <returns></returns>
    [HttpGet]
    public ResultModel PhoneDecrypt(string phoneNum)
    {
        var ret = new ResultModel();
        ret.code = 1;
        ret.data = Util.PhoneDecrypt(phoneNum);
        ret.msg = "操作成功";
        return ret;
    }
    #endregion

    #region 手机号加密
    /// <summary>
    /// 手机号加密
    /// </summary>
    /// <param name="phoneNum"></param>
    /// <returns></returns>
    [HttpGet]
    public ResultModel PhoneEncrypt(string phoneNum)
    {
        var ret = new ResultModel();
        ret.code = 1;
        ret.data = Util.PhoneEncrypt(phoneNum);
        ret.msg = "操作成功";
        return ret;
    }
    #endregion

    #region 获取服务器时间
    /// <summary>
    /// 获取服务器时间
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public ResultModel GetServerTime()
    {
        var ret = new ResultModel();
        ret.code = 1;
        ret.data = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        ret.msg = "操作成功";
        return ret;
    }
    #endregion

    #region 获取任务进度
    /// <summary>
    /// 获取任务进度
    /// </summary>
    /// <param name="taskKey">任务id 必填</param>
    /// <returns></returns>
    [HttpGet]
    public ResultModel<int> GetTaskProgress(string taskKey)
    {
        return OperationExecuteReturnData(() =>
        {
            return TaskManager.GetProgress(taskKey);
        });
    }
    #endregion

    #region 获取任务状态
    /// <summary>
    /// 获取任务状态
    /// </summary>
    /// <param name="taskKey">任务id 必填</param>
    /// <returns></returns>
    [HttpGet]
    public ResultModel<bool> TaskIsProcessing(string taskKey)
    {
        return OperationExecuteReturnData(() =>
        {
            return TaskManager.IsProcessing(taskKey);
        });
    } 
    #endregion

    #region 导入、导出任务

    /// <summary>
    /// 查询execl任务
    /// </summary>
    /// <param name="task">任务id(必填)</param>
    /// <returns></returns>
    [HttpGet]
    public ResultModel<ExcelTaskModel> QueryTask(string task)
    {
        ResultModel<ExcelTaskModel> result = new ResultModel<ExcelTaskModel>();
        if (excelTasks.ContainsKey(task) == true)
        {
            ExcelTaskModel taskmodel = excelTasks[task];
            result.code = 1;
            result.data = taskmodel;
        }
        else
        {
            result.code = -1;
            result.msg = "没有找到Excel执行任务";
        }

        return result;
    }

    /// <summary>
    /// 删除execl任务
    /// </summary>
    /// <param name="model">参考Schemas ExcelTaskDeleteModel对象
    /// 必填项[task]</param>
    /// <returns></returns>
    [HttpPost]
    public ResultModel DeleteTask(ExcelTaskDeleteModel model)
    {
        ResultModel result = new ResultModel();

        if (excelTasks.ContainsKey(model.task) == true)
        {
            lock (excelTasks)
            {
                excelTasks.Remove(model.task);
            }

            result.code = 1;
        }
        else
        {
            result.code = -1;
            result.msg = "没有找到Excel执行任务";
        }

        return result;
    }

    #endregion

    #region 根据类型获取字典列表
    /// <summary>
    /// 根据类型获取字典列表
    /// </summary>
    /// <param name="type">类型(必填)</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<ResultModel<List<Dictionary>>> GetDictionaryListByType(int type)
    {
        return await OperationExecuteReturnDataAsync(async () =>
        {
            return await _dictonaryBLL.GetListAsync(x => x.Type == type);
        });
    }
    #endregion
}
